﻿using Coldairarrow.Entity.Base;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Base
{
    public partial class Base_RegionBusiness : BaseBusiness<Base_Region>, IBase_RegionBusiness, ITransientDependency
    {
        public Base_RegionBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<Base_Region>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<Base_Region>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<Base_Region, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        } 
        public async Task<List<Base_RegionTreeDTO>> GetTreeDataListAsync(Base_RegionTreeInputDTO input)
        {
            var where = LinqHelper.True<Base_Region>();
            if (!input.Pid.IsNullOrEmpty())
                where = where.And(x => x.Pid == input.Pid);
            var list = await GetIQueryable().Where(where).ToListAsync();
            var treeList = list
                .Select(x => new Base_RegionTreeDTO
                {
                    Id = x.Id,
                    ParentId = x.Pid,
                    Text = x.Name,
                    Value = x.Id,
                }).ToList();

            return TreeHelper.BuildTree(treeList);
        }
        public async  Task<List<Base_Region>> GetAddressListAsync(string Pid, string Deep)
        {
            if (Deep == "1")
            {
                return await   Db.GetListBySqlAsync<Base_Region>("SELECT Id,NAME  FROM Base_Region WHERE Deep = '" + Deep + "'");
            }
            else if(!string.IsNullOrWhiteSpace(Deep)&& !string.IsNullOrWhiteSpace(Pid)) {
                return await Db.GetListBySqlAsync<Base_Region>("SELECT Id,NAME  FROM Base_Region WHERE Deep='" + Deep + "' and Pid='" + Pid+"'");
            }
            return  null;
        }



        public async Task<Base_Region> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(Base_Region data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(Base_Region data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

     



        #endregion

        #region 私有成员

        #endregion
    }
}